{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python格式化字符串的简单方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python有三种格式化字符串的方式：\n",
    "1. %-格式化\n",
    "2. str.format()\n",
    "3. f-Strings，超级好用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. %-格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"hoxis\"\n",
    "age = 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18 ?'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello, %s. you are %s ?\" %(name, age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"hoxis\"\n",
    "age = 18\n",
    "country = \"China\"\n",
    "hair = \"black\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18 ?. Your country is China, and your hair is black'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello, %s. you are %s ?. Your country is %s, and your hair is %s\" %(name, age, country,hair)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. str.format()\n",
    "从Python 2.6引入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18?'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 替换字段用大括号进行标记\n",
    "\"hello, {}. you are {}?\".format(name,age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18?'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以通过索引来以其他顺序引用变量\n",
    "\"hello, {1}. you are {0}?\".format(age,name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18?'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用key-value的键值对\n",
    "\"hello, {name}. you are {age1}?\".format(age1=age,name=name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18?'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接从字典读取数据\n",
    "person = {\"name\":\"hoxis\",\"age\":18}\n",
    "\"hello, {name}. you are {age}?\".format(**person)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello, hoxis. you are 18 ?. Your country is China, and your hair is black'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 依然会比较冗长\n",
    "\"hello, {}. you are {} ?. Your country is {}, and your hair is {}\".format(name, age, country,hair)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. f-Strings\n",
    "从Python3.6引入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hi, hoxis, are you 18'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以f字符开头，大括号直接使用变量\n",
    "f\"hi, {name}, are you {age}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'7'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接在大括号里执行计算\n",
    "f\"{ 2 * 3 + 1}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hoxis is handsome.'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接调用函数\n",
    "test = lambda x : x.lower()\n",
    "name = \"Hoxis\"\n",
    "f\"{test(name)} is handsome.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hoxis is handsome.'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接调用内置函数\n",
    "f\"{name.lower()} is handsome.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{74}'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意1：如果包含大括号，需要写多个\n",
    "f\"{{74}}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、实例对比-编写多行SQL语句\n",
    "编写SQL：实现输入某个班级的，成绩大于某个值的学生列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n    select \\n        sid,sname\\n    from sgrade\\n    where cid=030613 and grade>80\\n'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一种方式%s\n",
    "sql = \"\"\"\n",
    "    select \n",
    "        sid,sname\n",
    "    from sgrade\n",
    "    where cid=%s and grade>%d\n",
    "\"\"\"% ('030613', 80)\n",
    "sql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n    select \\n        sid,sname\\n    from sgrade\\n    where cid=030613 and grade>80\\n'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二种方式str.format\n",
    "sql = \"\"\"\n",
    "    select \n",
    "        sid,sname\n",
    "    from sgrade\n",
    "    where cid={cid} and grade>{grade}\n",
    "\"\"\".format(cid='030613', grade=80)\n",
    "sql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第三种方式f-string\n",
    "cid='030613'\n",
    "grade=80\n",
    "sql = f\"\"\"\n",
    "    select \n",
    "        sid,sname\n",
    "    from sgrade\n",
    "    where cid={cid} and grade>{grade}\n",
    "\"\"\"\n",
    "sql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
